package http_proxy_middleware

import (
	"gin-gateway/middleware"
	"gin-gateway/core/utils"
	"github.com/gin-gonic/gin"
)

// HTTPFlowCountMiddleware 流量统计 : 全站、服务、租户
func HTTPFlowCountMiddleware() gin.HandlerFunc {
	return func(c *gin.Context) {
		serviceDetail := GetServiceDetail(c)

		RedisAllFlowCounter, err := utils.FlowCountManager.GetRedisCounter(utils.FlowTotal) // 获取全站流量统计器
		if err != nil {
			middleware.ResponseError(c, 4001, err)
			c.Abort()
			return
		}
		RedisAllFlowCounter.Increase()

		RedisServiceCounter, err := utils.FlowCountManager.GetRedisCounter(utils.FlowServicePrefix + serviceDetail.Info.ServiceName) // 服务流量统计器
		if err != nil {
			middleware.ResponseError(c, 4001, err)
			c.Abort()
			return
		}
		RedisServiceCounter.Increase()

		//dayServiceCount, _ := serviceCounter.GetDayDataByRedis(time.Now())
		//fmt.Printf("serviceCounter qps:%v,dayCount:%v", serviceCounter.QPS, dayServiceCount)
		c.Next()
	}
}
